package com.example.demo.use_demo.a_http_pool.config;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author zhangqiang
 * @version 1.0
 * @date 2025/03/04
 */
@Configuration
public class HttpClientPoolConfig {

    @Bean
    public CloseableHttpClient createPooledClient() {
        PoolingHttpClientConnectionManager poolManager = new PoolingHttpClientConnectionManager();
        poolManager.setMaxTotal(200);
        poolManager.setDefaultMaxPerRoute(50);
        RequestConfig config = RequestConfig.custom()
                .setConnectTimeout(5000)
                .setSocketTimeout(10000)
                .build();
        return HttpClients.custom()
                .setConnectionManager(poolManager)
                .setDefaultRequestConfig(config)
                .build();
    }

}
